package com.se.service;

import com.se.dao.es.ActESDao;
import com.se.dao.mysql.ActDao;
import com.se.dao.mysql.CatDao;
import com.se.dao.mysql.ImgDao;
import com.se.dao.mysql.UserDao;
import com.se.domain.mysql.ActivityDO;
import com.se.domain.mysql.CategoryDO;
import com.se.domain.mysql.UserDO;
import com.se.domain.qo.ActESQO;
import com.se.domain.request.param.SearchActParam;
import com.se.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class ActSearchService {
    @Autowired
    UserDao userDao;
    @Autowired
    ActDao actDao;
    @Autowired
    ActESDao actESDao;
    @Autowired
    CatDao catDao;
    @Autowired
    ImgDao imgDao;

    public List<SearchActItemVO> searchAct(SearchActParam param, Long userId) throws IOException {
        List<SearchActItemVO> res = new ArrayList<>();
        List<ActESQO> esqoList = actESDao.searchAct(param);

        List<Long> actIds = new ArrayList<>();
        for (ActESQO esqo : esqoList) {
            actIds.add(esqo.getActId());
        }

        List<Long> userIds = new ArrayList<>();
        Set<Long> catIdSet = new HashSet<>();
        List<Long> imgIdList = new ArrayList<>();
        //填充活动信息
        Map<Long, ActivityDO> activityDOMap = actDao.mapByActIdList(actIds);
        for (ActESQO esqo : esqoList) {
            ActivityDO activityDO = activityDOMap.get(esqo.getActId());
            if(activityDO == null)
                continue;


            SearchActItemVO itemVO = new SearchActItemVO(new ActVO(activityDO));
//            itemVO.setUser(new UserVO(activityDO.getUserId()));
            itemVO.setUserId(activityDO.getUserId());
            itemVO.getAct().setTitle(esqo.getTitle());

            for (String catIdStr : activityDO.getCatIds().split(",")) {
                Long catId = Long.parseLong(catIdStr);
                catIdSet.add(catId);
                itemVO.getAct().getCatIds().add(catId);
            }

            for (Long imgId : activityDO.getImgIdList()) {
                imgIdList.add(imgId);
                itemVO.getAct().getImgIds().add(imgId);
            }

            userIds.add(activityDO.getUserId());

            res.add(itemVO);
        }

        //填充cat信息
        Map<Long, CatVO> catVOMap = catDao.mapVOByCatIds(catIdSet);
        for (SearchActItemVO itemVO : res) {
            for (Long catId : itemVO.getAct().getCatIds()) {
                CatVO catVO = catVOMap.get(catId);
                if(catVO == null)
                    continue;
                itemVO.getAct().getCats().add(catVO);
            }
        }

        //填充img信息
        Map<Long, ImgVO> imgVOMap = imgDao.mapVOByImgIdList(imgIdList);
        for (SearchActItemVO itemVO : res) {
            for (Long imgId : itemVO.getAct().getImgIds()) {
                ImgVO imgVO = imgVOMap.get(imgId);
                if(imgVO == null)
                    continue;
                itemVO.getAct().getImgs().add(imgVO);
            }
        }

        //填充user信息
        Map<Long, UserVO> userVOMap = userDao.mapVOByUserIds(userIds);
        for (SearchActItemVO itemVO : res) {
            UserVO userVO = userVOMap.get(itemVO.getUserId());
            userVO.checkSelf(userId);
            itemVO.setUser(userVO);

        }



        return res;
    }
}
